<template>
  <div class="role-management">
    <el-card>
      <div class="header">
        <h2>角色管理</h2>
        <el-button type="primary" @click="showAddDialog">添加角色</el-button>
      </div>

      <el-table :data="roles" border style="width: 100%">
        <el-table-column prop="name" label="角色名称" width="180"></el-table-column>
        <el-table-column prop="description" label="描述"></el-table-column>
        <el-table-column prop="permissions" label="权限">
          <template #default="{row}">
            <el-tag v-for="permission in row.permissions" :key="permission.id" style="margin-right: 5px;">
              {{ permission.name }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template #default="{row}">
            <el-button size="mini" @click="handleEdit(row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog v-model="dialogVisible" :title="dialogTitle">
      <el-form :model="currentRole" label-width="80px">
        <el-form-item label="角色名称">
          <el-input v-model="currentRole.name"></el-input>
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="currentRole.description" type="textarea"></el-input>
        </el-form-item>
        <el-form-item label="权限">
          <el-select v-model="currentRole.permissions" multiple placeholder="请选择权限">
            <el-option
              v-for="permission in allPermissions"
              :key="permission.id"
              :label="permission.name"
              :value="permission.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import api from '../api'

const roles = ref([])
const allPermissions = ref([])
const dialogVisible = ref(false)
const dialogTitle = ref('添加角色')
const currentRole = ref({
  id: null,
  name: '',
  description: '',
  permissions: []
})

const fetchRoles = async () => {
  try {
    const response = await api.get('/roles')
    roles.value = response.roles
  } catch (error) {
    console.error('获取角色列表失败:', error)
  }
}

const fetchPermissions = async () => {
  try {
    const response = await api.get('/permissions')
    allPermissions.value = response.permissions
  } catch (error) {
    console.error('获取权限列表失败:', error)
  }
}

const showAddDialog = () => {
  dialogTitle.value = '添加角色'
  currentRole.value = {
    id: null,
    name: '',
    description: '',
    permissions: []
  }
  dialogVisible.value = true
}

const handleEdit = (role) => {
  dialogTitle.value = '编辑角色'
  currentRole.value = {
    id: role.id,
    name: role.name,
    description: role.description,
    permissions: role.permissions.map(p => p.id)
  }
  dialogVisible.value = true
}

const handleDelete = async (role) => {
  try {
    await api.delete(`/roles/${role.id}`)
    await fetchRoles()
  } catch (error) {
    console.error('删除角色失败:', error)
  }
}

const handleSubmit = async () => {
  try {
    if (currentRole.value.id) {
      await api.put(`/roles/${currentRole.value.id}`, currentRole.value)
    } else {
      await api.post('/roles', currentRole.value)
    }
    dialogVisible.value = false
    await fetchRoles()
  } catch (error) {
    console.error('保存角色失败:', error)
  }
}

onMounted(() => {
  fetchRoles()
  fetchPermissions()
})
</script>

<style scoped>
.role-management {
  padding: 20px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}
</style>
